from typing import Annotated

from markupsafe import escape

from fastapi import Cookie
from pydantic import BaseModel, model_validator

from app.utils import types


class Cookies(BaseModel):
    sid: str
    """session id"""
    aid: str
    """account id"""
    ts: int
    """登录时间戳"""

    @model_validator(mode='before')
    @classmethod
    def before(cls, data):
        data = types.safe_dict(data)
        data['sid'] = escape(data.get('sid', ''))
        data['aid'] = escape(data.get('aid', ''))
        data['ts'] = types.safe_int(data.get('ts', None), -1)
        return data

    @model_validator(mode='after')
    def after(self):
        return self


CookieDep = Annotated[Cookies, Cookie()]
